var fSerch = {
	currentPage: 1,
	totalPage: 1
};
$(function() {
	//初始化数据
	getstru();
	getAllUser();
	var page = 1;
	var userList = [];
	firSercth();
		upAndDown();
	$(document).on("click", "#modifyUser", function() {
		modifyBtn($(this).attr("data-userId"));
	});
	$("span[title=监狱]").click(function() {
		
		$(".child").html("");
		str = '<option value="choice">请选择分监区</option>';
		$(".child").append(str);
		$(".department").html("");
		str = '<option value="choice">请选择科室或者监区</option>';
		$(".department").append(str);
		getstru();
		
		firSercth();
	});
	

	$(".department").change(function() {
		$(".child").html("");
		str = '<option value="choice">请选择分监区</option>';
		$(".child").append(str);
		var structureId = $(this).val();
		if (structureId != "choice") {
			firSercth();
			if ($(".department :selected").attr("data-num") == '0') {
				YuXi.util.get("getAllStructureBySuperId.do", {
					superId: structureId
				}, function(data) {
					var str = "";
					var object = data.object;
					$(".child").html("");
					str = '<option value="choice">请选择分监区</option>';
					$(".child").append(str);
					for (var i = 0; i < object.length; i++) {
						str = '<option value="' + object[i].id + '">' + object[i].name + '</option>';
						$(".child").append(str);
					}

				}, function() {

				});
			}
			
		}
	});
	$(".child").change(function() {
		var structureId = $(this).val();
		if (structureId != "choice") {
			
			firSercth();
		}
	});
	$(document).on("click", ".btn-user-enable", function(data) {
		var id = $(this).attr("data-id");
		myTool.alert({
			title: "启用账户",
			context: "你确定要启用当前用户吗?",
			callback: function() {
				YuXi.util.post("enableUserById.do", {
					userId: id
				}, function(data) {
					if (data.status) {
						myTool.alert({
							title: data.subType,
							context: data.msg
						});
						window.location.reload();
					} else {
						myTool.alert({
							title: data.subType,
							context: data.msg
						});
					}
				}, function() {
					myTool.alert({
						title: "提示",
						context: "连接超时"
					})
				});
			}
		});
	});
	//分页，首页
	$("#headPage").click(function() {
			fSerch.currentPage = 1;
			if (fSerch.currentPage == 1) {
				return false;
			}
			$("#totalPage").html(fSerch.currentPage + "/" + fSerch.totalPage);
			getAllUser(fSerch);
		})
		//分页，尾页
	$("#endPage").click(function() {
			if (fSerch.currentPage == fSerch.totalPage) {
				return false;
			}
			$("#totalPage").html(fSerch.totalPage + "/" + fSerch.totalPage);
			fSerch.currentPage = fSerch.totalPage;
			getAllUser(fSerch);
		})
		//分页，上一页
	$("#prevPage").click(function() {
			if (fSerch.currentPage <= 1) {
				return false;
			} else {
				fSerch.currentPage = fSerch.currentPage - 1;
				$("#totalPage").html(fSerch.currentPage + "/" + fSerch.totalPage);
			}
			getAllUser(fSerch);

		})
		//分页，下一页
	$("#nextPage").click(function() {

		if (fSerch.currentPage >= fSerch.totalPage) {
			return false;
		} else {
			fSerch.currentPage = fSerch.currentPage + 1;
			$("#totalPage").html(fSerch.currentPage + "/" + fSerch.totalPage);
		}
		getAllUser(fSerch);

	})


	//获取部门及职务





/*根据当前角色所属部门Id获取他下级的所有部门*/
function getstru() {
	var url="getDepartmentAndArea.do";//获取部门
	var data={};
	var object={};
	data=YuXi.util.get(url,null,function(data){
		object=data.object;
		for(var i=0;i<object.length;i++)
		{
			var html='<option data-num='+object[i].num+' class="struct-select-option" value="'+object[i].id+'" data-id="'+object[i].id+'">'+object[i].name+'</option>';
			$(".department").append(html);
		}
	

	}, function() {});
}




/*获取所有用户*/
function getAllUser(data) {
	YuXi.util.get("getUserList.do", data, function(data) {
		var userList = {};

		fSerch.totalPage = data.totalPage;
		fSerch.currentPage = data.currentPage;
		$("#totalPage").html(fSerch.currentPage + "/" + fSerch.totalPage);
		userList = data.list;

		var htm = "";
		$("tbody").html("");
		for (var i = 0; i < userList.length; i++) {

			var userStr = '&nbsp;<span class="btn btn-xs btn-danger" onClick="stopModel(' + userList[i].userId + ')">停用</span>';
			if (!userList[i].isEnable) {
				userStr = '&nbsp;<span class="btn btn-xs btn-primary btn-user-enable" data-id="' + userList[i].userId + '">启用</span>';
			}
			htm += '<tr>' +
				'<td>' + (i + 1) + '</td>' +
				'<td>' + userList[i].userName + '</td>' +
				'<td>' + userList[i].userAccount + '</td>' +
				'<td>' + userList[i].idNumber + '</td>' +
				'<td>' + userList[i].roleName + '</td>' +
				'<td>' + userList[i].structName + '</td>' +
				'<td>' + userList[i].positionName + '</td>' +
				'<td>' +
				'<a href="javascript:void(0)" data-id='+userList[i].userId+' class="btn btn-primary btn-stop btn-xs up">上移</a>'+
				'<a href="javascript:void(0)" data-id='+userList[i].userId+' class="btn btn-info btn-stop btn-xs dowm">下移</a>'+
				'&nbsp;<button class="btn btn-xs btn-primary" id="modifyUser" data-userId='+userList[i].userId+'>查看并修改</button>'+
				userStr +
				'</td></tr>';
		}
		$("tbody").append(htm);
	
	}, function() {
		
	})

}
//确认顺序
$("#sureOrder").click(function(){
	var url="";//确认顺序的提交的接口
	var rs=[];
	$("#tableSort>tbody").find("tr").each(function(i){
		var userId=$(this).find(".up").attr("data-id");
		var item={
			order:i,
			userId:userId
		}
		rs.push(item);
	})
	YuXi.util.get(url,rs,function(data){
		if(data.status)
		{
			myTool.alert({
				title: "提示",
				context: "提交成功!"
			});
		}		
			
	},function(){});//请求数据	

})

//上移和下移
function upAndDown()
{
	//上移
	$(document).on("click",".up",function(){
		 var $tr = $(this).parent().parent(); 
		 	
		 	if ($tr.index() != 0) 
		 	{ 
	           
	            $tr.prev().before($tr); 

	            
       		 } 
		 
	
	})
   
    //下移
    var $dowm = $(".dowm"); 
    var len = $dowm.length; 
  	$(document).on("click",".dowm",function(){ 
         var $tr = $(this).parent().parent(); 
	        if ($tr.index() != len - 1) { 
	            
	            $tr.next().after($tr);
	            
	          
	        } 
	    
	  
    }); 
}





/*第一排查询*/
function firSercth() {

	//获取数据

	var structureId;
	  if($(".child").val()!="choice"){
		  structureId=$(".child :selected").val();
		 
	  }else if($(".department").val()!="choice"){
		 
		 structureId=$(".department :selected").val();
		
	  }else{
		  structureId=1;
		
	  }
	
	fSerch.structureId =structureId;
	if($("input[name=account]").val()=="请输入用户名"){
		fSerch.userAccountOrName=null;
	}else{
		fSerch.userAccountOrName=$("input[name=account]").val();
	}

	fSerch.currentPage = 1;
	getAllUser(fSerch);
}


/*跳转到修改页面*/
function modifyBtn(id) {

	window.location.href = 'userModify.jsp?detailId=' + id;

}
/*停用模态框*/
function stopModel(stopid) {
	delId = stopid;
	$("#stopModel").modal('show');
}
/*停用用户*/
function stopUsrBtn() {
	$("#stopModel").modal('hide');
	var deleteId = delId;
	var url = "stopUserById.do"; //停用用户
	YuXi.util.post(url, {
		userId: deleteId
	}, function(data) {
		if (data.status) {
			myTool.alert({
				title: data.subType,
				context: data.msg
			});
			window.location.reload();
		} else {
			myTool.alert({
				title: data.subType,
				context: data.msg
			});
		}

	}, function() {
		//console.log("停用失败");
	})
}

$("#firstSearch").click(function() {
	firSercth();
});
//简介，分级，渠道，状态，时间（今天,一周...）查找

});